package com.ITArmy.controller.admin;

import com.ITArmy.beans.Question;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.controller.base.BaseController;
import com.ITArmy.service.QuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Api(value = "后台问题接口层次", tags = "后台问题接口层次Controller层")
@Controller
@RequestMapping("/admin/Question")
public class AdminQuestionController extends BaseController {

    @RequestMapping("/list")
    @ApiOperation(value = "遍历问题信息",
            httpMethod = "POST", response = ServerResponse.class)
    public String list(@RequestParam(value = "page", defaultValue = "1") int page, Model model) {
        PageHelper.startPage(page,9);
        List<Question> questions = questionService.findAllQuestion();
        PageInfo PageInfo = new PageInfo<>(questions,9);
        model.addAttribute("questionList", PageInfo);
        return "question";
    }

    @RequestMapping("/edit")
    @ResponseBody
    @ApiOperation(value = "修改信息时显示原始信息",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse edit(Long id) {
        return questionService.findQuestionByIdByBack(id);
    }

    @RequestMapping("/update")
    @ResponseBody
    @ApiOperation(value = "修改问题信息，", notes = "管理员可修改下面除了ID的所有的参数，用户只能修改标题，内容，类型,赏金,问题是否解决",
            httpMethod = "POST", response = ServerResponse.class)
    public ServerResponse updateQuestion(Question question) {
        return questionService.updateQuestion(question);
    }

    @RequestMapping("/findQuestionByTitle")
    @ApiOperation(value = "根据标题模糊查询，",
            httpMethod = "POST", response = ServerResponse.class)
    public String findQuestionByTitle(String title, Model model) {
        ServerResponse questionServerResponse = questionService.findQuestionByTitle(title);
        model.addAttribute("questionListByTitle", questionServerResponse);
        return "question";
    }

    @RequestMapping("/findQuestionByType")
    @ApiOperation(value = "根据博客类型搜索，", notes = "类型用固定选择",
            httpMethod = "POST", response = ServerResponse.class)
    public String findQuestionByType(Integer tid, Model model) {
        ServerResponse questionServerResponse = questionService.findQuestionByType(tid);
        model.addAttribute("questionListByType", questionServerResponse);
        return "question";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @ApiOperation(value = "删除问题信息",
            httpMethod = "DELETE", response = ServerResponse.class)
    public ServerResponse delete(Long id) {
        return questionService.deleteQuestionById(id);
    }

    @RequestMapping("/deleteMany")
    @ResponseBody
    @ApiOperation(value = "批量删除问题信息",
            httpMethod = "DELETE", response = ServerResponse.class)
    public ServerResponse deleteMany(String ids) {
        String[] id = ids.split(",");
        return questionService.deleteManyByIds(id);
    }
}
